define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'scanwork/process_material/index' + location.search,
                    add_url: 'scanwork/process_material/add',
                    edit_url: 'scanwork/process_material/edit',
                    del_url: 'scanwork/process_material/del',
                    multi_url: 'scanwork/process_material/multi',
                    table: 'scanwork_process_material',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'model.product.name', title: '产品名称', operate: 'LIKE'},
                        {field: 'model.name', title: '型号名称', operate: 'LIKE'},
                        {field: 'process.name', title: '工序名称', operate: 'LIKE'},
                        {field: 'material.name', title: '物料名称', operate: 'LIKE'},
                        {field: 'material.code', title: '物料编码', operate: 'LIKE'},
                        {field: 'quantity', title: '用量', operate:'BETWEEN', sortable: true, formatter: function(value, row, index) {
                            return value + ' ' + (row.material ? row.material.unit : '');
                        }},
                        {field: 'loss_rate', title: '损耗率(%)', operate:'BETWEEN', sortable: true, formatter: function(value, row, index) {
                            return value + '%';
                        }},
                        {field: 'is_required', title: '是否必需', searchList: {"0":'非必需',"1":'必需'}, formatter: function(value, row, index) {
                            return value == 1 ? '<span class="label label-success">必需</span>' : '<span class="label label-default">非必需</span>';
                        }},
                        {field: 'createtime', title: __('创建时间'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'operate', title: __('操作'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        batchConfig: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
